// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
let request = require('request')
let cheerio = require('cheerio'); //爬虫
let iconv = require('iconv-lite'); //处理gbk编码的网页
// import UserAgent from 'user-agents';
let UserAgent = require('user-agents');
// let Entities = require('html-entities').XmlEntities;
// let entities = new Entities();

/**
 * 查询价格
 * @param {string} startPlace
 * @param {string} endPlace
 * @param {number} weight
 * @param {string} startTime
 */
function requestPrice(startPlace, endPlace, weight, startTime) {	
  return new Promise((resolve, reject) => {
    const userAgent = new UserAgent().toString();
    console.log('---- userAgent ----:', userAgent);
    const url = 'https://m.kuaidi100.com/apicenter/kdquerytools.do?method=priceandtimenew';
    request({
      url: url,
      method: 'POST',
      // body: 'startPlace=广东省-珠海市-香洲区&endPlace=湖南省-娄底市-新化县&weight=2&startTime=2024-3-26 20:29&platform=MWWW&coname=uc',
      body: `startPlace=${startPlace}&endPlace=${endPlace}&weight=${weight}&startTime=${startTime}&platform=MWWW&coname=uc`,
      headers: {
        'User-Agent': userAgent,
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
      }
      // encoding: null //设置抓取页面时不要对数据做任何转换
    }, function(err, res, body) {
      if (err) {
        reject(err)
      } else {
        resolve(body)
      }
    });
  })
}

/**
 * @param {number} company
 * @param {string} keyword
 * @param {number} pageIndex
 * @param {number} size
 */
function requestWd(company, keyword, pageIndex = 0, size = 10) {
  console.log('---- requestWd----:', `method=searchnetwork&company=${company}&keyword=${keyword}&offset=${size * pageIndex}&size=${size}`)
  return new Promise((resolve, reject) => {
    const userAgent = new UserAgent().toString();
    console.log('---- userAgent ----:', userAgent);
    const url = 'https://m.kuaidi100.com/network/www/searchapi.do';
    request({
      url: url,
      method: 'POST',
	  // body: 'method=searchnetwork&company=10&keyword=广东省珠海市高新区唐家湾镇&offset=0&size=10',
      body: `method=searchnetwork&company=${company}&keyword=${keyword}&offset=${size * pageIndex}&size=${size}`,
      headers: {
        'User-Agent': userAgent,
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
      }
      // encoding: null //设置抓取页面时不要对数据做任何转换
    }, function(err, res, body) {
      if (err) {
        reject(err)
      } else {
        resolve(body)
      }
    });
  })
}

module.exports = {
	_before: function () { // 通用预处理器

	},
	/**
	 * method1方法描述
	 * @param {string} param1 参数1描述
	 * @returns {object} 返回值描述
	 */
	/* 
	method1(param1) {
		// 参数校验，如无参数则不需要
		if (!param1) {
			return {
				errCode: 'PARAM_IS_NULL',
				errMsg: '参数不能为空'
			}
		}
		// 业务逻辑
		
		// 返回结果
		return {
			param1 //请根据实际需要返回值
		}
	}
	*/
   
  /**
   * @param {string} startPlace
   * @param {string} endPlace
   * @param {number} weight
   * @param {string} startTime
   */
   getPrice: async function(startPlace, endPlace, weight, startTime) {
	   const res = await requestPrice(startPlace, endPlace, weight, startTime);
	   console.log('---- getPrice ----', res);
	   return res;
   },
   
   getWd: async function(company, keyword, offset = 0, size = 10) {
	   const res = await requestWd(company, keyword, offset, size);
	   console.log('---- getWd ----', res);
	   return res;
   }
}
